var api = require('../../config/api.js');
const app = getApp()

Page({
  data: {
    motto: 'Hello World',
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    userInfo: {
      nickName: '点击登录',
      avatarUrl: 'http://yanxuan.nosdn.127.net/8945ae63d940cc42406c3f67019c5cb6.png'
    },
    order: {
      unpaid: 0,
      unship: 0,
      unrecv: 0,
      uncomment: 0
    },
    hasLogin: false
  },
  addQuestion:function(e){
    if (this.data.hasLogin) {
      let tab = e.currentTarget.dataset.index
      let route = e.currentTarget.dataset.route
      try {
        wx.setStorageSync('tab', tab);
      } catch (e) {
      }
      wx.navigateTo({
        url: route,
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
    } else {
      wx.showToast({
        title: '您未登陆',
        icon: "none",
        duration: 1500
      });
    };
  },
  onLoad: function () {
    if (app.globalData.userInfo) {
      this.hasLogin=true
      console.log("1")
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true,
        hasLogin: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        console.log("2")
        this.hasLogin = true
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true,
          hasLogin: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          console.log("3")
          this.hasLogin = true
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true,
            hasLogin : true
          })
        }
      })
    }
    if (this.hasLogin){
      console.log("1231 ")
    }else{
      console.log("123123123121 ")
    }
  },
  
  getUserInfo: function(e) {
    console.log("-->",e)
    app.globalData.userInfo = e.detail.userInfo
    if (e.detail.errMsg !="getUserInfo:ok"){
      this.setData({
        hasUserInfo: false,
        hasLogin: false
      })
    }else{
      this.setData({
        userInfo: e.detail.userInfo,
        hasUserInfo: true,
        hasLogin: true
      })
    }
    
  },
  onReady: function () {

  },
  linkme: function () {
    wx.showToast({
      title: '不给你联系方式',
      icon: 'none',     //默认值是success,就算没有icon这个值，就算有其他值最终也显示success
      duration: 2000,      //停留时间
    })
  },
  onShow: function () {

    //获取用户的登录信息
    if (app.globalData.hasLogin) {
      let userInfo = wx.getStorageSync('userInfo');
      console.log("登陆了的")
      this.setData({
        userInfo: userInfo,
        hasLogin: true
      });

      let that = this;
      util.request(api.UserIndex).then(function (res) {
        if (res.errno === 0) {
          that.setData({
            order: res.data.order
          });
        }
      });
    }

  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭
  },
  goLogin() {
    wx.getUserInfo({
      success: function (res) {
        that.setData({
          nickName: res.userInfo.nickName,
          avatarUrl: res.userInfo.avatarUrl,
        })
      },
    })

  },
  goOrder:function(){
    wx.showToast({
      title: '看啥呢？',
      icon: "none",
      duration: 1500
    });
  },
  goCoupon() {
    if (this.data.hasLogin) {
      wx.showToast({
        title: '目前不支持',
        icon: 'none',
        duration: 2000
      });
      // wx.navigateTo({
      //   url: "/pages/ucenter/couponList/couponList"
      // });
    } else {
      wx.showToast({
        title: '目前不支持',
        icon: 'none',
        duration: 2000
      });
      // wx.navigateTo({
      //   url: "/pages/auth/login/login"
      // });
    };
  },
  goGroupon() {
    if (this.data.hasLogin) {
      wx.showToast({
        title: '目前不支持',
        icon: 'none',
        duration: 2000
      });
      // wx.navigateTo({
      //   url: "/pages/groupon/myGroupon/myGroupon"
      // });
    } else {
      wx.showToast({
        title: '目前不支持',
        icon: 'none',
        duration: 2000
      });
      // wx.navigateTo({
      //   url: "/pages/auth/login/login"
      // });
    };
  },
  exitLogin: function () {
    wx.showModal({
      title: '',
      confirmColor: '#b4282d',
      content: '退出登录？',
      success: function (res) {
        if (!res.confirm) {
          return;
        }

        util.request(api.AuthLogout, {}, 'POST');
        app.globalData.hasLogin = false;
        wx.removeStorageSync('token');
        wx.removeStorageSync('userInfo');
        wx.reLaunch({
          url: '/pages/index/index'
        });
      }
    })

  }
})
